<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="业务类型" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="请选择业务类型" clearable>
          <el-option label="收货入库" value="RECEIVE" />
          <el-option label="上架" value="PUTAWAY" />
          <el-option label="拣货" value="PICK" />
          <el-option label="出库" value="OUTBOUND" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="logList" border stripe>
      <el-table-column label="日志号" align="center" prop="logNo" width="160" />
      <el-table-column label="业务类型" align="center" prop="bizType" width="100">
        <template slot-scope="scope">
          <el-tag :type="getBizTypeTag(scope.row.bizType)" size="small">
            {{ getBizTypeText(scope.row.bizType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="业务单号" align="center" prop="bizNo" width="160" />
      <el-table-column label="商品名称" align="center" prop="productName" min-width="180" show-overflow-tooltip />
      <el-table-column label="批次号" align="center" prop="batchCode" width="120" />
      <el-table-column label="储位" align="center" prop="locationCode" width="100" />
      <el-table-column label="数量" align="center" prop="quantity" width="100">
        <template slot-scope="scope">
          <span :style="{ color: scope.row.operType === 'IN' ? '#67C23A' : '#F56C6C' }">
            {{ scope.row.operType === 'IN' ? '+' : '-' }}{{ scope.row.quantity }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operator" width="100" />
      <el-table-column label="操作时间" align="center" prop="operTime" width="160" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
  </div>
</template>

<script>
import { listStockLog } from '@/api/wms/stock'

export default {
  name: 'StockLog',
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      logList: [],
      queryParams: { pageNum: 1, pageSize: 10, bizType: null, productName: null }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listStockLog(this.queryParams).then(response => {
        this.logList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    getBizTypeTag(type) {
      const map = { 'RECEIVE': 'success', 'PUTAWAY': 'primary', 'PICK': 'warning', 'OUTBOUND': 'danger' }
      return map[type] || 'info'
    },
    getBizTypeText(type) {
      const map = { 'RECEIVE': '收货入库', 'PUTAWAY': '上架', 'PICK': '拣货', 'OUTBOUND': '出库' }
      return map[type] || type
    }
  }
}
</script>
